package com.atguigu.easyexcel.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.atguigu.easyexcel.entity.StudentExcelData;
import lombok.Data;

import java.util.ArrayList;
import java.util.List;
@Data
public class StudentExcelDataListener extends AnalysisEventListener<StudentExcelData> {
    List<StudentExcelData> data = new ArrayList<>() ;
    int limit = 1000 ;
    int count = 0 ;
    @Override
    public void invoke(StudentExcelData excelData, AnalysisContext context) {
        data.add(excelData) ; //每次读取一行excel数据然后调用此回调方法
        if (data.size()==limit) {
            System.out.println("开始批量保存数据到数据库");
            data.clear();
            count++ ;
            System.out.println(count);
        }
    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        System.out.println("execl读取完毕！");
        if (data.size() > 0) {
            data.clear();
        }
    }
}
